package com.htgd.gardenexpo.service.impl;

import com.htgd.gardenexpo.dao.*;
import com.htgd.gardenexpo.entity.GeConPortraitRules;
import com.htgd.gardenexpo.service.IGeConPortraitRulesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htgd.gardenexpo.utils.CalculationUtils;
import com.htgd.gardenexpo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import static com.htgd.gardenexpo.utils.DateUtils.getQuarter;

/**
 * <p>
 * 画像规则 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-10
 */
@Service
public class GeConPortraitRulesServiceImpl extends ServiceImpl<GeConPortraitRulesMapper, GeConPortraitRules> implements IGeConPortraitRulesService {

    @Autowired
    private WxUserAchievementMapper wxUserAchievementMapper;

    @Autowired
    private GeConUserCompeteMapper geConUserCompeteMapper;

    @Autowired
    private WxUserStepMapper wxUserStepMapper;

    @Autowired
    private GeConFreeSportsMapper geConFreeSportsMapper;

    @Override
    public MyMonthPortraitVO getMyMonthPortrait(String openId) {
        MyMonthPortraitVO monthPortraitVO = new MyMonthPortraitVO();

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取上个月的日期
        LocalDate lastMonth = currentDate.minusMonths(1);
        LocalDate lastTwoMonth = currentDate.minusMonths(2);
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        // 格式化输出
        String lastMonthStr = lastMonth.format(formatter);
        String lastTwoMonthStr = lastTwoMonth.format(formatter);


        //成就数据
        MyPortraitAchievementDataVO myPortraitAchievementDataVO = new MyPortraitAchievementDataVO();
        List<MyAchievementItemVO> achievementList = wxUserAchievementMapper.getMonthOpenId(openId, lastMonthStr);
        myPortraitAchievementDataVO.setAchievementList(achievementList);
        myPortraitAchievementDataVO.setAchievementNum(achievementList.size());
        monthPortraitVO.setMyPortraitAchievementDataVO(myPortraitAchievementDataVO);

        //微信步数数据
        MyPortraitWxStepDataVO myPortraitWxStepDataVO = new MyPortraitWxStepDataVO();
        long totalStep = wxUserStepMapper.getMonthTotalNum(openId, lastMonthStr);
        long step = wxUserStepMapper.getMonthAvgNum(openId, lastMonthStr);

        myPortraitWxStepDataVO.setStep(step);
        myPortraitWxStepDataVO.setTotalStep(totalStep);

        //对比
        long step2 = wxUserStepMapper.getMonthAvgNum(openId, lastTwoMonthStr);
        String subtractAndFormat = CalculationUtils.subtractAndFormat(step2, step);
        myPortraitWxStepDataVO.setStepComparison(subtractAndFormat);

        //公里与马拉松
        double distance = totalStep * 0.6 / 1000;
        double marathon = totalStep * 0.6 / 1000/42;

        // 如果距离为0，直接设置为0.0
        if (distance > 0) {
            DecimalFormat df = new DecimalFormat("#.00");
            String formattedDistance = df.format(distance);
            myPortraitWxStepDataVO.setDistance(formattedDistance);
        }else {
            myPortraitWxStepDataVO.setDistance("0");
        }

        if (marathon > 0) {
            DecimalFormat df = new DecimalFormat("#.0");
            String formattedMarathon = df.format(marathon);
            myPortraitWxStepDataVO.setMarathon(formattedMarathon);
        }else {
            myPortraitWxStepDataVO.setMarathon("0");
        }


        //最多的一天
        Map<String,Object> max = wxUserStepMapper.getMonthDayDateBySort(openId, lastMonthStr,"desc");
        if(max!=null){
            myPortraitWxStepDataVO.setMaxStep((Integer) max.get("step"));
            myPortraitWxStepDataVO.setMaxDate((String) max.get("date"));
        }

        //最少的一天
        Map<String,Object> min = wxUserStepMapper.getMonthDayDateBySort(openId, lastMonthStr,"asc");
        if(min!=null){
            myPortraitWxStepDataVO.setMinStep((Integer) min.get("step"));
            myPortraitWxStepDataVO.setMinDate((String) min.get("date"));
        }

        monthPortraitVO.setMyPortraitWxStepDataVO(myPortraitWxStepDataVO);

        //赛事数据
        MyPortraitCompetitionChallengeDataVO myPortraitCompetitionChallengeDataVO = new MyPortraitCompetitionChallengeDataVO();
        int monthCompetitionNum = geConUserCompeteMapper.getMonthCompetitionNum(openId, lastMonthStr);
        int monthChallengeNum = geConUserCompeteMapper.getMonthChallengeNum(openId, lastMonthStr);

        myPortraitCompetitionChallengeDataVO.setCompetitionNum(monthCompetitionNum);
        myPortraitCompetitionChallengeDataVO.setChallengeNum(monthChallengeNum);

        //对比
        int monthCompetitionCompareNum = geConUserCompeteMapper.getMonthCompetitionNum(openId, lastTwoMonthStr);
        int monthChallengeCompareNum = geConUserCompeteMapper.getMonthChallengeNum(openId, lastTwoMonthStr);
        String subtractAndFormat1 = CalculationUtils.subtractAndFormat(monthCompetitionNum, monthCompetitionCompareNum);
        String subtractAndFormat2 = CalculationUtils.subtractAndFormat(monthChallengeNum, monthChallengeCompareNum);
        myPortraitCompetitionChallengeDataVO.setCompetitionCompareNum(subtractAndFormat1);
        myPortraitCompetitionChallengeDataVO.setChallengeCompareNum(subtractAndFormat2);

        monthPortraitVO.setMyPortraitCompetitionChallengeDataVO(myPortraitCompetitionChallengeDataVO);

        //自由跑数据
        MyPortraitFreeSportsDataVO myPortraitFreeSportsDataVO = new MyPortraitFreeSportsDataVO();
        int monthFreeSportsNum = geConFreeSportsMapper.getMonthNum(openId,lastMonthStr);
        int monthFreeSportsCompareNum = geConFreeSportsMapper.getMonthNum(openId, lastTwoMonthStr);
        String subtractAndFormat3 = CalculationUtils.subtractAndFormat(monthFreeSportsNum, monthFreeSportsCompareNum);

        myPortraitFreeSportsDataVO.setFreeNum(monthFreeSportsNum);
        myPortraitFreeSportsDataVO.setFreeCompareNum(subtractAndFormat3);
        monthPortraitVO.setMyPortraitFreeSportsDataVO(myPortraitFreeSportsDataVO);

        return monthPortraitVO;
    }

    @Override
    public MyQuarterPortraitVO getMyQuarterPortrait(String openId) {
        MyQuarterPortraitVO myQuarterPortraitVO = new MyQuarterPortraitVO();

        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取当前年份
        int currentYear = today.getYear();
        // 获取当前季度
        int currentQuarter = getQuarter(today.getMonthValue());
        // 计算上个季度
        int lastQuarter = currentQuarter == 1 ? 4 : currentQuarter - 1;
        // 获取上一个季度的年份
        int lastQuarterYear = (currentQuarter == 1) ? currentYear - 1 : currentYear;

        // 计算上上个季度
        int lastLastQuarter = (lastQuarter == 1) ? 4 : lastQuarter - 1;
        // 获取上上个季度的年份
        int lastLastQuarterYear = (lastQuarter == 1) ? lastQuarterYear - 1 : lastQuarterYear;



        //成就数据
        MyPortraitAchievementDataVO myPortraitAchievementDataVO = new MyPortraitAchievementDataVO();
        List<MyAchievementItemVO> achievementList = wxUserAchievementMapper.getQuarterByOpenId(openId, lastQuarterYear, lastQuarter);
        myPortraitAchievementDataVO.setAchievementList(achievementList);
        myPortraitAchievementDataVO.setAchievementNum(achievementList.size());
        myQuarterPortraitVO.setMyPortraitAchievementDataVO(myPortraitAchievementDataVO);

        //微信步数数据
        MyPortraitWxStepDataVO myPortraitWxStepDataVO = new MyPortraitWxStepDataVO();
        long totalStep = wxUserStepMapper.getQuarterTotalNum(openId, lastQuarterYear, lastQuarter);
        long step = wxUserStepMapper.getQuarterAvgNum(openId, lastQuarterYear, lastQuarter);
        myPortraitWxStepDataVO.setStep(step);
        myPortraitWxStepDataVO.setTotalStep(totalStep);

        //对比
        long step2 = wxUserStepMapper.getQuarterAvgNum(openId, lastLastQuarterYear, lastLastQuarter);
        String subtractAndFormat = CalculationUtils.subtractAndFormat(step2, step);
        myPortraitWxStepDataVO.setStepComparison(subtractAndFormat);

        //公里与马拉松
        double distance = totalStep * 0.6 / 1000;
        double marathon = totalStep * 0.6 / 1000/42;

        // 如果距离为0，直接设置为0.0
        if (distance > 0) {
            DecimalFormat df = new DecimalFormat("#.00");
            String formattedDistance = df.format(distance);
            myPortraitWxStepDataVO.setDistance(formattedDistance);
        }else {
            myPortraitWxStepDataVO.setDistance("0");
        }

        if (marathon > 0) {
            DecimalFormat df = new DecimalFormat("#.0");
            String formattedMarathon = df.format(marathon);
            myPortraitWxStepDataVO.setMarathon(formattedMarathon);
        }else {
            myPortraitWxStepDataVO.setMarathon("0");
        }


        //最多的一天
        Map<String,Object> max = wxUserStepMapper.getQuarterDayDateBySort(openId, lastQuarterYear, lastQuarter, "desc");
        if(max!=null){
            myPortraitWxStepDataVO.setMaxStep((Integer) max.get("step"));
            myPortraitWxStepDataVO.setMaxDate((String) max.get("date"));
        }

        //最少的一天
        Map<String,Object> min = wxUserStepMapper.getQuarterDayDateBySort(openId, lastQuarterYear, lastQuarter,"asc");
        if(min!=null){
            myPortraitWxStepDataVO.setMinStep((Integer) min.get("step"));
            myPortraitWxStepDataVO.setMinDate((String) min.get("date"));
        }

        myQuarterPortraitVO.setMyPortraitWxStepDataVO(myPortraitWxStepDataVO);

        //赛事数据
        MyPortraitCompetitionChallengeDataVO myPortraitCompetitionChallengeDataVO = new MyPortraitCompetitionChallengeDataVO();
        int quarterCompetitionNum = geConUserCompeteMapper.getQuarterCompetitionNum(openId, lastQuarterYear, lastQuarter);
        int quarterChallengeNum = geConUserCompeteMapper.getQuarterChallengeNum(openId, lastQuarterYear, lastQuarter);

        myPortraitCompetitionChallengeDataVO.setCompetitionNum(quarterCompetitionNum);
        myPortraitCompetitionChallengeDataVO.setChallengeNum(quarterChallengeNum);

        int quarterCompetitionCompareNum = geConUserCompeteMapper.getQuarterCompetitionNum(openId, lastLastQuarterYear, lastLastQuarter);
        int quarterChallengeCompareNum = geConUserCompeteMapper.getQuarterChallengeNum(openId, lastLastQuarterYear, lastLastQuarter);
        String subtractAndFormat1 = CalculationUtils.subtractAndFormat(quarterCompetitionNum, quarterCompetitionCompareNum);
        String subtractAndFormat2 = CalculationUtils.subtractAndFormat(quarterChallengeNum, quarterChallengeCompareNum);
        myPortraitCompetitionChallengeDataVO.setCompetitionCompareNum(subtractAndFormat1);
        myPortraitCompetitionChallengeDataVO.setChallengeCompareNum(subtractAndFormat2);

        myQuarterPortraitVO.setMyPortraitCompetitionChallengeDataVO(myPortraitCompetitionChallengeDataVO);


        //自由跑数据
        MyPortraitFreeSportsDataVO myPortraitFreeSportsDataVO = new MyPortraitFreeSportsDataVO();
        int quarterFreeSportsNum = geConFreeSportsMapper.getQuarterNum(openId,lastQuarterYear,lastQuarter);
        int quarterFreeSportsCompareNum = geConFreeSportsMapper.getQuarterNum(openId, lastLastQuarterYear,lastLastQuarter);
        String subtractAndFormat3 = CalculationUtils.subtractAndFormat(quarterFreeSportsNum, quarterFreeSportsCompareNum);

        myPortraitFreeSportsDataVO.setFreeNum(quarterFreeSportsNum);
        myPortraitFreeSportsDataVO.setFreeCompareNum(subtractAndFormat3);
        myQuarterPortraitVO.setMyPortraitFreeSportsDataVO(myPortraitFreeSportsDataVO);
        return myQuarterPortraitVO;
    }

    @Override
    public MyYearPortraitVO getMyYearPortrait(String openId) {
        MyYearPortraitVO myYearPortraitVO = new MyYearPortraitVO();

        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取当前年份
        int currentYear = today.getYear();
        // 获取去年年份
        int lastYear = currentYear - 1;
        // 获取上上年份
        int lastLastYear = lastYear - 1;


        //成就数据
        MyPortraitAchievementDataVO myPortraitAchievementDataVO = new MyPortraitAchievementDataVO();
        List<MyAchievementItemVO> achievementList = wxUserAchievementMapper.getYearByOpenId(openId,lastYear);
        myPortraitAchievementDataVO.setAchievementList(achievementList);
        myPortraitAchievementDataVO.setAchievementNum(achievementList.size());
        myYearPortraitVO.setMyPortraitAchievementDataVO(myPortraitAchievementDataVO);

        //微信步数数据
        MyPortraitWxStepDataVO myPortraitWxStepDataVO = new MyPortraitWxStepDataVO();
        long totalStep = wxUserStepMapper.getYearTotalNum(openId, lastYear);
        long step = wxUserStepMapper.getYearAvgNum(openId, lastYear);
        myPortraitWxStepDataVO.setStep(step);
        myPortraitWxStepDataVO.setTotalStep(totalStep);

        //对比
        long step2 = wxUserStepMapper.getYearAvgNum(openId,lastLastYear);
        String subtractAndFormat = CalculationUtils.subtractAndFormat(step2, step);
        myPortraitWxStepDataVO.setStepComparison(subtractAndFormat);

        //公里与马拉松
        double distance = totalStep * 0.6 / 1000;
        double marathon = totalStep * 0.6 / 1000/42;

        // 如果距离为0，直接设置为0.0
        if (distance > 0) {
            DecimalFormat df = new DecimalFormat("#.00");
            String formattedDistance = df.format(distance);
            myPortraitWxStepDataVO.setDistance(formattedDistance);
        }else {
            myPortraitWxStepDataVO.setDistance("0");
        }

        if (marathon > 0) {
            DecimalFormat df = new DecimalFormat("#.0");
            String formattedMarathon = df.format(marathon);
            myPortraitWxStepDataVO.setMarathon(formattedMarathon);
        }else {
            myPortraitWxStepDataVO.setMarathon("0");
        }


        //最多的一天
        Map<String,Object> max = wxUserStepMapper.getYearDayDateBySort(openId, lastYear, "desc");
        if(max!=null){
            myPortraitWxStepDataVO.setMaxStep((Integer) max.get("step"));
            myPortraitWxStepDataVO.setMaxDate((String) max.get("date"));
        }

        //最少的一天
        Map<String,Object> min = wxUserStepMapper.getYearDayDateBySort(openId, lastYear, "asc");
        if(min!=null){
            myPortraitWxStepDataVO.setMinStep((Integer) min.get("step"));
            myPortraitWxStepDataVO.setMinDate((String) min.get("date"));
        }

        myYearPortraitVO.setMyPortraitWxStepDataVO(myPortraitWxStepDataVO);

        //赛事数据
        MyPortraitCompetitionChallengeDataVO myPortraitCompetitionChallengeDataVO = new MyPortraitCompetitionChallengeDataVO();
        int yearCompetitionNum = geConUserCompeteMapper.getYearCompetitionNum(openId, lastYear);
        int yearChallengeNum = geConUserCompeteMapper.getYearChallengeNum(openId, lastYear);
        myPortraitCompetitionChallengeDataVO.setCompetitionNum(yearCompetitionNum);
        myPortraitCompetitionChallengeDataVO.setChallengeNum(yearChallengeNum);

        int yearCompetitionCompareNum = geConUserCompeteMapper.getYearCompetitionNum(openId, lastLastYear);
        int yearChallengeCompareNum = geConUserCompeteMapper.getYearChallengeNum(openId, lastLastYear);
        String subtractAndFormat1 = CalculationUtils.subtractAndFormat(yearCompetitionNum, yearCompetitionCompareNum);
        String subtractAndFormat2 = CalculationUtils.subtractAndFormat(yearChallengeNum, yearChallengeCompareNum);
        myPortraitCompetitionChallengeDataVO.setCompetitionCompareNum(subtractAndFormat1);
        myPortraitCompetitionChallengeDataVO.setChallengeCompareNum(subtractAndFormat2);

        myYearPortraitVO.setMyPortraitCompetitionChallengeDataVO(myPortraitCompetitionChallengeDataVO);

        //自由跑数据
        MyPortraitFreeSportsDataVO myPortraitFreeSportsDataVO = new MyPortraitFreeSportsDataVO();
        int yearFreeSportsNum = geConFreeSportsMapper.getYearNum(openId,lastYear);
        int yearFreeSportsCompareNum = geConFreeSportsMapper.getYearNum(openId,lastLastYear);
        String subtractAndFormat3 = CalculationUtils.subtractAndFormat(yearFreeSportsNum, yearFreeSportsCompareNum);

        myPortraitFreeSportsDataVO.setFreeNum(yearFreeSportsNum);
        myPortraitFreeSportsDataVO.setFreeCompareNum(subtractAndFormat3);
        myYearPortraitVO.setMyPortraitFreeSportsDataVO(myPortraitFreeSportsDataVO);
        return myYearPortraitVO;
    }
}
